package com.ruoyi.order.service.impl;

import com.ruoyi.order.domain.Evaluate;
import com.ruoyi.order.domain.EvaluateImage;
import com.ruoyi.order.api.domain.Order;
import com.ruoyi.order.mapper.EvaluateImageMapper;
import com.ruoyi.order.mapper.EvaluateMapper;
import com.ruoyi.order.service.IEvaluateImageService;
import com.ruoyi.order.service.IEvaluateService;
import com.ruoyi.order.service.IOrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Arrays;
import java.util.Collections;
import java.util.List;

/**
 * 订单评价Service业务层处理
 * 
 * @author yun
 * @date 2024-12-31
 */
@Service
public class EvaluateServiceImpl implements IEvaluateService 
{
    @Autowired
    private EvaluateMapper evaluateMapper;
    @Autowired
    private IEvaluateImageService evaluateImageService;
    @Autowired
    private EvaluateImageMapper evaluateImageMapper;
    @Autowired
    private IOrderService orderService;

    /**
     * 查询订单评价
     * 
     * @param id 订单评价主键
     * @return 订单评价
     */
    @Override
    public Evaluate selectEvaluateById(Long id)
    {
        Evaluate evaluate = evaluateMapper.selectEvaluateById(id);

//        添加评论照片对象列表
        setEvaluateImg(evaluate);
//        添加订单数据
        if(evaluate.getOrderId() != null){
            setOrder(evaluate);
        }

        return evaluate;
    }

    /**
     * 查询订单评价列表
     * 
     * @param evaluate 订单评价
     * @return 订单评价
     */
    @Override
    public List<Evaluate> selectEvaluateList(Evaluate evaluate)
    {
        List<Evaluate> list = evaluateMapper.selectEvaluateList(evaluate);
        for (int i = 0; i < list.size(); i++) {
            list.set(i, setEvaluateImg(list.get(i)));
            list.set(i, setOrder(list.get(i)));
        }
        return list;
    }

    @Override
    public List<Evaluate> selectByShopId(Long shopId) {
        if(shopId == null || shopId < 0){
            return null;
        }
        return evaluateMapper.selectByShopId(shopId);
    }

    /**
     * 新增订单评价
     * 
     * @param evaluate 订单评价
     * @return 结果
     */
    @Transactional
    @Override
    public int insertEvaluate(Evaluate evaluate)
    {
        int i = 0;
        if(evaluate != null){
            i = evaluateMapper.insertEvaluate(evaluate);
//            添加照片到数据库
            insertImg(evaluate);
//            修改订单状态
            Long orderId = evaluate.getOrderId();
            if(orderId != null && orderId > 0){
                Order order = new Order();
                order.setId(orderId);
//                修改状态为已评价
                order.setState(5L);
                orderService.updateOrder(order);
            }
        }
        return i;
    }

    /**
     * 修改订单评价
     * 
     * @param evaluate 订单评价
     * @return 结果
     */
    @Transactional
    @Override
    public int updateEvaluate(Evaluate evaluate)
    {
        int i = 0;
        if(evaluate != null){
            i = evaluateMapper.updateEvaluate(evaluate);
//            添加照片到数据库
            if(evaluate.getIsImgChanged()){
//                删除之前的照片
                evaluateImageMapper.deleteEvaluateImageByEvaluateId(evaluate.getId());
                insertImg(evaluate);
            }
        }
        return i;
    }

    /**
     * 批量删除订单评价
     * 
     * @param ids 需要删除的订单评价主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteEvaluateByIds(Long[] ids)
    {
//        先删除子表的数据
        for (Long id : ids) {
            evaluateImageMapper.deleteEvaluateImageByEvaluateId(id);
        }
//        再删除主表的数据
        return evaluateMapper.deleteEvaluateByIds(ids);

    }

    /**
     * 删除订单评价信息
     * 
     * @param id 订单评价主键
     * @return 结果
     */
    @Override
    public int deleteEvaluateById(Long id)
    {
        return evaluateMapper.deleteEvaluateById(id);
    }


//    添加信息--评论照片列表
    private Evaluate setEvaluateImg(Evaluate evaluate) {
        //        添加评论照片对象列表
        EvaluateImage evaluateImage = new EvaluateImage();
        evaluateImage.setEvaluateId(evaluate.getId());

        List<EvaluateImage> evaluateImageList = evaluateImageService.selectEvaluateImageList(evaluateImage);
        evaluate.setImgItemList(evaluateImageList);
        StringBuilder imgList = new StringBuilder();
        for (int i = 0; i < evaluateImageList.size(); i++) {
            imgList.append(evaluateImageList.get(i).getPhoto());
            if(i != evaluateImageList.size()-1){
                imgList.append(",");
            }
        }

        evaluate.setImgList(imgList.toString());
        return evaluate;
    }
//    添加信息--order订单
    private Evaluate setOrder(Evaluate evaluate){
        Order order = orderService.selectOrderById(evaluate.getOrderId());
        evaluate.setOrder(order);
        return evaluate;
    }


//     添加数据库数据--添加照片列表
    private void insertImg(Evaluate item){
        EvaluateImage img = new EvaluateImage();
        img.setEvaluateId(item.getId());

//        添加照片列表
        String str = item.getImgList();
        if(str != null && !str.isEmpty()){
            str = str.replace("\"", "");
            List<String> list = Arrays.asList(str.split(","));
            list.forEach(s->{
                if(s!= null && !s.isEmpty()){
                    img.setPhoto(s);
                    evaluateImageService.insertEvaluateImage(img);
                }
            });

        }

    }



}
